//
// Created by 49647 on 2022/4/10.
//

#ifndef MYHANDLER_AHANDLER_H
#define MYHANDLER_AHANDLER_H

#include <memory>

struct AMessage;
struct ALooper;

struct AHandler : public std::enable_shared_from_this<AHandler> {
protected:
    AHandler() = default;
    virtual ~AHandler() = default;
    virtual void onMessageReceived(const std::shared_ptr<AMessage> &msg) = 0;

private:
    friend struct ALooper;

    void setLooper(const std::weak_ptr<ALooper> &looper) {
        mLooper = looper;
    }

    friend struct AMessage;

    std::weak_ptr<ALooper> getLooper() const {
        return mLooper;
    }

    void deliverMessage(const std::shared_ptr<AMessage> &msg) {
        onMessageReceived(msg);
    }

private:
    std::weak_ptr<ALooper> mLooper;
};


#endif //MYHANDLER_AHANDLER_H
